//
//  CollectionViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/6.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "CollectionViewController.h"
#import "UserModel.h"
#import "TravelnotesViewController.h"

@interface CollectionViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UIButton *backButton;

@end

@implementation CollectionViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *nameString = [userDefaults objectForKey:@"userName"];
    NSMutableArray *array = [userDefaults objectForKey:nameString];
    self.dataSource = [NSMutableArray arrayWithArray:array];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.titleView];
    [self.titleView addSubview:self.backButton];
    [self.view addSubview:self.tableView];
}

#pragma mark -- action/callback

- (void)backButtonTaped:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TravelnotesViewController *travelVC = [[TravelnotesViewController alloc] init];
    travelVC.urlString = self.dataSource[indexPath.row][@"urlString"];
    travelVC.noteTitle = self.dataSource[indexPath.row][@"noteTitle"];
    [self.navigationController pushViewController:travelVC animated:YES];
}

// 删除表格行
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataSource removeObjectAtIndex:indexPath.row];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *nameString = [userDefaults objectForKey:@"userName"];
        [userDefaults setObject:self.dataSource forKey:nameString];
        [userDefaults synchronize];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataSource[indexPath.row][@"noteTitle"];
    
    return cell;
}

#pragma mark -- getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 80) style:UITableViewStylePlain];
            tableView.backgroundColor = [UIColor whiteColor];
            tableView.delegate = self;
            tableView.dataSource = self;
            tableView.rowHeight = 70;
            [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
            tableView.tableFooterView = [[UIView alloc] init];
            tableView;
        });
    }
    return _tableView;
}

- (UIView *)titleView {
    if (!_titleView) {
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 80)];
        _titleView.backgroundColor = [UIColor whiteColor];
    }
    return _titleView;
}

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(10, 30, 60, 40);
            [button setImage:[UIImage imageNamed:@"返回按钮.png"] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(backButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _backButton;
}

@end
